package com.android.ddfafu.ui.changepwd;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.android.ddfafu.R;
import com.android.ddfafu.activity.Login_activity;
import com.android.ddfafu.domian.Data;
import com.android.ddfafu.domian.ResultInfo;
import com.android.ddfafu.utils.Const;
import com.android.ddfafu.utils.UserHttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChangeFragment extends Fragment {

    private ChangeViewModel changeViewModel;
    private EditText old_password;
    private EditText new_password;
    private EditText confirm_password;
    private Button changePwd_btn;
    private SharedPreferences sp;

    public View onCreateView(@NonNull LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        changeViewModel =
                ViewModelProviders.of(this).get(ChangeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_change, container, false);
        old_password = root.findViewById(R.id.old_password);
        new_password = root.findViewById(R.id.new_password);
        confirm_password = root.findViewById(R.id.confirm_password);
        changePwd_btn = root.findViewById(R.id.change_btn);
        sp = getActivity().getSharedPreferences("userData", Context.MODE_PRIVATE);

        changePwd_btn.setOnClickListener(v->{
            String oldPwd = old_password.getText().toString();
            String now_password = sp.getString("PASSWORD", "");
            if (!oldPwd.equals(now_password)){
                Toast.makeText(getActivity(), "输入的原密码错误", Toast.LENGTH_SHORT).show();
                return;
            }

            String newPwd = new_password.getText().toString();
            String confirmPwd = confirm_password.getText().toString();

            if (newPwd.equals(oldPwd)){
                Toast.makeText(getActivity(), "新密码不能与原密码相同", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!newPwd.equals(confirmPwd)){
                Toast.makeText(getActivity(), "两次输入的密码不相同", Toast.LENGTH_SHORT).show();
                return;
            }
            String token =sp.getString("TOKEN","");
            String username =sp.getString("USERNAME","");
            UserHttpUtil.changeWithOkHttp(Const.SERVER_URL_CHANGE, token, username, newPwd, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Toast.makeText(getActivity(), "服务器错误", Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //得到服务器返回的具体内容
                    final String responseData = response.body().string();
                    System.out.println(responseData);
                    ObjectMapper mapper = new ObjectMapper();
                    final ResultInfo info = mapper.readValue(responseData, ResultInfo.class);
                    getActivity().runOnUiThread(()->{
                        if (info.isFlag() == false){
                            Toast.makeText(getActivity(), info.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                        if (info.isFlag() == true){
                            Toast.makeText(getActivity(), info.getMsg(), Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getActivity(),Login_activity.class);
                            sp.edit().putBoolean("REMEMBER",false);
                            sp.edit().apply();
                            startActivity(intent);
                        }
                    });

                }
            });


        });

        return root;
    }
}